import time


def singleton(clsname):
    instances = {}

    def getinstance(*args, **kwargs):
        if clsname not in instances:
            instances[clsname] = clsname(*args, **kwargs)
        return instances[clsname]

    return getinstance


def datestr_to_sec(date_str, is_msec=False):
    """
    日期字符串转秒数
    1900-01-10 => 23532
    """
    if not date_str:
        return None
    # 接收毫秒数
    if isinstance(date_str, (int)):
        if is_msec:
            return date_str
        return date_str / 1000
    format_str = "%Y-%m-%d"
    if len(date_str.split(' ')) > 1 and ':' in date_str:
        format_str = '%Y-%m-%d %H:%M:%S'
    if "/" in date_str:
        date_str = date_str.replace("/", "-")
    birthday = time.strptime(date_str, format_str)
    if is_msec:
        return time.mktime(birthday) * 1000
    return time.mktime(birthday)
